package com.zhiqian.bean;

import com.zhiqian.enums.ResponseCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class RpcResponse<T> implements Serializable {
    /**
     * 响应状态码
     */
    private Integer statusCode;

    /**
     * 响应状态补充信息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;

    /**
     * 快速生成成功的响应对象
     * @param data
     * @param <T>
     * @return
     */
    public static <T>RpcResponse<T> success(T data){
        RpcResponse<T> response=new RpcResponse<>();
        response.setData(data);
        response.setStatusCode(ResponseCode.SUCCESS.getCode());
        return response;
    }

    /**
     * 快速生成失败的响应对象
     * @param code
     * @param <T>
     * @return
     */
    public static <T>RpcResponse<T> fail(ResponseCode code){
        RpcResponse<T> response=new RpcResponse<>();
        response.setStatusCode(code.getCode());
        response.setMessage(code.getMessage());
        return response;
    }

}
